<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" :inline="true" label-width="68px">

      <el-form-item label="模板名称" prop="postName">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入岗位名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['permission:post:post']"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['permission:post:{id}:put']"
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['myapp:stockmodel:{id}:delete']"
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>

      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="模板名称" align="center" prop="name" width="180"/>
      <el-table-column label="状态" align="center" prop="state" :formatter="statusFormat" width="100"/>

      <el-table-column label="最大市值股票名称" align="center" prop="symbol_single_stock" width="100"/>
      <el-table-column label="最大个股市值" align="center" prop="mv_single_stock" />
      <el-table-column label="最大个股市值占比" align="center" prop="ratio_single_stock" />
      <el-table-column label="最大股本占比股票名称" align="center" prop="symbol_share_hold" />
      <el-table-column label="最大股本占比股票市值" align="center" prop="mv_share_hold" />
      <el-table-column label="最大股本占比股票占股本比例" align="center" prop="ratio_share_hold" />
      <el-table-column label="最大单一行业权重行业名称" align="center" prop="industry" />
      <el-table-column label="最大单一行业市值" align="center" prop="mv_industry" />
      <el-table-column label="最大单一行业权重比率" align="center" prop="ratio_industry" />
      <el-table-column label="总市值" align="center" prop="mv_sum" />
      <el-table-column label="深市总市值" align="center" prop="mv_sze" />
      <el-table-column label="沪市总市值" align="center" prop="mv_sse" />
      <el-table-column label="深市总市值" align="center" prop="mv_cyb" />
      <el-table-column label="沪市总市值" align="center" prop="mv_kcb" />
      <el-table-column label="深市市值占比" align="center" prop="ratio_mv_sze" />
      <el-table-column label="沪市市值占比" align="center" prop="ratio_mv_sse" />
      <el-table-column label="深市市值占比" align="center" prop="ratio_mv_cyb" />
      <el-table-column label="沪市市值占比" align="center" prop="ratio_mv_kcb" />
      <el-table-column label="当日持股数量" align="center" prop="cnt_stock_hold" />
      <el-table-column label="前日持股数量" align="center" prop="cnt_stock_hold_prev" />
      <el-table-column label="当日委托总数" align="center" prop="cnt_order" />
      <el-table-column label="当日撤单总数" align="center" prop="cnt_cncl" />
      <el-table-column label="当日撤单率" align="center" prop="ratio_cncl" />
      <el-table-column label="当日交易额" align="center" prop="tovr" />
      <el-table-column label="当日换手率" align="center" prop="ratio_tovr" />
      <el-table-column label="当日撤单率(金额)" align="center" prop="ratio_cncl_tovr" />
      <el-table-column label="对价单撤单率(次数)" align="center" prop="ratio_cross_cncl" />
      <el-table-column label="限价单撤单率(次数)" align="center" prop="ratio_limit_cncl" />
      <el-table-column label="对价单/限价单" align="center" prop="ratio_cross_div_limit" />

      <el-table-column label="创建时间" align="center" prop="create_datetime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.create_datetime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        v-if="hasPermi(['myapp:stockmodel:{id}:put', 'myapp:stockmodel:{id}:delete'])"
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="180"
      >
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['myapp:stockmodel:{id}:put']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            v-hasPermi="['myapp:stockmodel:{id}:delete']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="200px">

        <el-form-item label="模板名称" prop="name">
          <el-input v-model="form.name" placeholder="模板名称" />
        </el-form-item>

        <el-form-item label="状态" prop="state">
          <el-radio-group v-model="form.state">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{ dict.dictLabel }}</el-radio>
          </el-radio-group>
        </el-form-item>


        <el-form-item label="最大市值股票名称" prop="symbol_single_stock">
          <el-input v-model="form.symbol_single_stock" placeholder="最大市值股票名称" />
        </el-form-item>
        <el-form-item label="最大个股市值" prop="mv_single_stock">
          <el-input v-model="form.mv_single_stock" placeholder="最大个股市值" />
        </el-form-item>
        <el-form-item label="最大个股市值占比" prop="ratio_single_stock">
          <el-input v-model="form.ratio_single_stock" placeholder="最大个股市值占比" />
        </el-form-item>
        <el-form-item label="最大股本占比股票名称" prop="symbol_share_hold">
          <el-input v-model="form.symbol_share_hold" placeholder="最大股本占比股票名称" />
        </el-form-item>
        <el-form-item label="最大股本占比股票市值" prop="mv_share_hold">
          <el-input v-model="form.mv_share_hold" placeholder="最大股本占比股票市值" />
        </el-form-item>
        <el-form-item label="最大股本占比股票占股本比例" prop="ratio_share_hold">
          <el-input v-model="form.ratio_share_hold" placeholder="最大股本占比股票占股本比例" />
        </el-form-item>
        <el-form-item label="最大单一行业权重行业名称" prop="industry">
          <el-input v-model="form.industry" placeholder="最大单一行业权重行业名称" />
        </el-form-item>
        <el-form-item label="最大单一行业市值" prop="mv_industry">
          <el-input v-model="form.mv_industry" placeholder="最大单一行业市值" />
        </el-form-item>
        <el-form-item label="最大单一行业权重比率" prop="ratio_industry">
          <el-input v-model="form.ratio_industry" placeholder="最大单一行业权重比率" />
        </el-form-item>
        <el-form-item label="总市值" prop="mv_sum">
          <el-input v-model="form.mv_sum" placeholder="总市值" />
        </el-form-item>
        <el-form-item label="深市总市值" prop="mv_sze">
          <el-input v-model="form.mv_sze" placeholder="深市总市值" />
        </el-form-item>
        <el-form-item label="沪市总市值" prop="mv_sse">
          <el-input v-model="form.mv_sse" placeholder="沪市总市值" />
        </el-form-item>
        <el-form-item label="深市总市值" prop="mv_cyb">
          <el-input v-model="form.mv_cyb" placeholder="深市总市值" />
        </el-form-item>
        <el-form-item label="沪市总市值" prop="mv_kcb">
          <el-input v-model="form.mv_kcb" placeholder="沪市总市值" />
        </el-form-item>
        <el-form-item label="深市市值占比" prop="ratio_mv_sze">
          <el-input v-model="form.ratio_mv_sze" placeholder="深市市值占比" />
        </el-form-item>
        <el-form-item label="沪市市值占比" prop="ratio_mv_sse">
          <el-input v-model="form.ratio_mv_sse" placeholder="沪市市值占比" />
        </el-form-item>
        <el-form-item label="深市市值占比" prop="ratio_mv_cyb">
          <el-input v-model="form.ratio_mv_cyb" placeholder="深市市值占比" />
        </el-form-item>
        <el-form-item label="沪市市值占比" prop="ratio_mv_kcb">
          <el-input v-model="form.ratio_mv_kcb" placeholder="沪市市值占比" />
        </el-form-item>
        <el-form-item label="当日持股数量" prop="cnt_stock_hold">
          <el-input v-model="form.cnt_stock_hold" placeholder="当日持股数量" />
        </el-form-item>
        <el-form-item label="前日持股数量" prop="cnt_stock_hold_prev">
          <el-input v-model="form.cnt_stock_hold_prev" placeholder="前日持股数量" />
        </el-form-item>
        <el-form-item label="当日委托总数" prop="cnt_order">
          <el-input v-model="form.cnt_order" placeholder="当日委托总数" />
        </el-form-item>
        <el-form-item label="当日撤单总数" prop="cnt_cncl">
          <el-input v-model="form.cnt_cncl" placeholder="当日撤单总数" />
        </el-form-item>
        <el-form-item label="当日撤单率" prop="ratio_cncl">
          <el-input v-model="form.ratio_cncl" placeholder="当日撤单率" />
        </el-form-item>
        <el-form-item label="当日交易额" prop="tovr">
          <el-input v-model="form.tovr" placeholder="当日交易额" />
        </el-form-item>
        <el-form-item label="当日换手率" prop="ratio_tovr">
          <el-input v-model="form.ratio_tovr" placeholder="当日换手率" />
        </el-form-item>
        <el-form-item label="当日撤单率(金额)" prop="ratio_cncl_tovr">
          <el-input v-model="form.ratio_cncl_tovr" placeholder="当日撤单率(金额)" />
        </el-form-item>
        <el-form-item label="对价单撤单率(次数)" prop="ratio_cross_cncl">
          <el-input v-model="form.ratio_cross_cncl" placeholder="对价单撤单率(次数)" />
        </el-form-item>
        <el-form-item label="限价单撤单率(次数)" prop="ratio_limit_cncl">
          <el-input v-model="form.ratio_limit_cncl" placeholder="限价单撤单率(次数)" />
        </el-form-item>
        <el-form-item label="对价单/限价单" prop="ratio_cross_div_limit">
          <el-input v-model="form.ratio_cross_div_limit" placeholder="对价单/限价单" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listType,getType,addType,updateType,delType} from "@/api/myapp/stockmodel"

export default {
  name: "stockmodelinstz",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态数据字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,

        name: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "模板名称不能为空", trigger: "blur" }
        ],
        state: [
          { required: true, message: "状态不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_normal_disable").then(response => {
      console.log( response.data)
      this.statusOptions = response.data;
    });
  },
  methods: {
    /** 查询岗位列表 */
    getList() {
      this.loading = true;
      listType(this.queryParams).then(response => {
        this.postList = response.data.results;
        this.total = response.data.count;
        this.loading = false;
      });
    },
    // 岗位状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions,row.state);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        postCode: undefined,
        postName: undefined,
        postSort: 0,
        status: this.selectDictDefault(this.statusOptions),
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加岗位";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getType(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改岗位";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateType(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addType(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const postIds = row.id || this.ids;
      this.$confirm('是否确认删除岗位编号为"' + postIds + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return delType(postIds);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有岗位数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return exportPost(queryParams);
      }).then(response => {
        this.download(response.data.file_url, response.data.name);
      });
    }
  }
};
</script>
<style scoped>
.el-table-column{
  width: 250px;
}
</style>
